package com.cryptape.cita_wallet.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.MapUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.cryptape.cita_wallet.CytonApplication;
import com.cryptape.cita_wallet.bean.MortgageAssociation;
import com.cryptape.cita_wallet.bean.MortgageAssociationBean;
import com.cryptape.cita_wallet.net.callback.CommonCallback;
import com.cryptape.cita_wallet.net.request.HttpRequestMode;
import com.cryptape.cita_wallet.service.http.TokenService;
import com.cryptape.cita_wallet.util.RSAUtils;
import com.cryptape.cita_wallet.util.db.DBWalletUtil;
import com.cryptape.cita_wallet.view.CompressEditText;
import com.cryptape.cita_wallet.view.SpacesItemDecoration;
import com.mangoworld.android.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import rx.Subscriber;

;

/**
 * Created by dengming on 13/1/20.
 */

public class MangoAssociationActivity extends NBaseActivity {
    private RecyclerView recyclerView;
    private Adapter mAdapter;
    //    private TextView tv_yesterdayPool;
//    private TextView tv_soCalculateForce_value;
//    private TextView tv_shareTheWorkForce_value;
//    private TextView teamCalculateForce;
//    private TextView tv_hashrate_yesterday;
//    private TextView tv_yesterdayStimulate_value;
//    private TextView tv_yesterdaySoCalculateForce_value;
    private MortgageAssociationBean mData;
    private Button advanced_setup_button;
    private CompressEditText cet_address;
    private TextView tv_yesterday_money;
    private TextView tv_yesterdayPushMoney;
    private TextView tv_yesterdayTeamMoney;
    private TextView tv_yesterdayLightNodeMoney;
    private TextView tv_my_share_exponent;
    private TextView tv_myFloor;
    private TextView tv_lightNodeFlag;
    private TextView tv_amount;
    private TextView tv_value;

//    private List<MortgageAssociation.Team> teamDatas = new ArrayList<>();

    @Override
    protected int getContentLayout() {
        return R.layout.activity_mango_aassociation;
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.mango_recycler);
//        tv_yesterdayPool = findViewById(R.id.tv_myFloor);
//        tv_soCalculateForce_value = findViewById(R.id.tv_yesterdayPushMoney);
//        tv_shareTheWorkForce_value = findViewById(R.id.tv_yesterdayTeamMoney);
//        teamCalculateForce = findViewById(R.id.tv_yesterdayTeamMoney);
//        tv_yesterdaySoCalculateForce_value = findViewById(R.id.tv_lightNodeFlag);
//        tv_hashrate_yesterday = findViewById(R.id.tv_teamValue);
//        tv_yesterdayForce_value = findViewById(R.id.tv_yesterdayForce_value);
//        tv_yesterdayStimulate_value = findViewById(R.id.tv_yesterdayStimulate_value);
//        mData.add(new MangoItemBean("MID11111","0x111111111111111"));
//        mData.add(new MangoItemBean("MID11111","0x111111111111111"));
//        mData.add(new MangoItemBean("MID11111","0x111111111111111"));
        advanced_setup_button = (Button) findViewById(R.id.advanced_setup_button);
        cet_address = findViewById(R.id.cet_address);
        tv_yesterday_money = (TextView) findViewById(R.id.tv_yesterday_money);
        tv_yesterdayPushMoney = (TextView) findViewById(R.id.tv_yesterdayPushMoney);
        tv_yesterdayTeamMoney = (TextView) findViewById(R.id.tv_yesterdayTeamMoney);
        tv_yesterdayLightNodeMoney = (TextView) findViewById(R.id.tv_yesterdayLightNodeMoney);
        tv_my_share_exponent = (TextView) findViewById(R.id.tv_my_share_exponent);
        tv_myFloor = (TextView) findViewById(R.id.tv_myFloor);
        tv_lightNodeFlag = (TextView) findViewById(R.id.tv_lightNodeFlag);
        tv_amount = (TextView) findViewById(R.id.tv_amount);
        tv_value = (TextView) findViewById(R.id.tv_value);

        EditText et = cet_address.findViewById(R.id.et);
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString())) {
                    if (mData != null) {
                        List<MortgageAssociation.Team> localTeams = mData.getTeamList();
                        if (localTeams != null) {
//                            teams = localTeams;
                            teams.clear();
                            teams.addAll(localTeams);
                            if (mAdapter != null) {
                                mAdapter.notifyDataSetChanged();
                            }
                        }
                    }
                }
            }
        });
        initAdapter();
    }

    @Override
    protected void initData() {
        String address = DBWalletUtil.getCurrentWallet(CytonApplication.getInstance()).address;
        try {
            Map params = MapUtils.newHashMap();
            params.put("address", address);
            String jsonData = GsonUtils.toJson(params);
            String content = RSAUtils.encrypt(jsonData);
            HttpRequestMode.getMortgage(MangoAssociationActivity.this, content, true, new CommonCallback() {
                @Override
                public void onSuccess(Object data, String successMsg, int responseSuccessCode) {
                    if (data == null) {
                        return;
                    }
                    mData = (MortgageAssociationBean) data;


                    tv_yesterday_money.setText(mData.getYesterdayMoney());
                    tv_yesterdayPushMoney.setText(mData.getYesterdayPushMoney());
                    tv_yesterdayTeamMoney.setText(mData.getYesterdayTeamMoney());
                    tv_yesterdayLightNodeMoney.setText(mData.getYesterdayLightNodeMoney());
                    tv_my_share_exponent.setText(mData.getMyPushPro());
                    tv_myFloor.setText(mData.getMyFloor() + "");
                    tv_lightNodeFlag.setText(mData.getLightNodeFlag() == 1 ? getString(R.string.yes) : getString(R.string.no));
                    tv_amount.setText(mData.getTeamNum() + "MGP");
                    tv_value.setText("$" + mData.getTeamValue());
                    List<MortgageAssociation.Team> localTeams = mData.getTeamList();
                    if (localTeams != null) {
                        teams.clear();
                        teams.addAll(localTeams);
                        mAdapter.notifyDataSetChanged();
                    }
                }

                @Override
                public void onFailure(String msg, int responseFailCode) {
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void initAction() {
//        advanced_setup_button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                Toast.makeText(MangoAssociationActivity.this, getString(R.string.search_failed), Toast.LENGTH_SHORT).show();
//
//            }
//        });

        //                                Toast.makeText(MangoAssociationActivity.this,getString(R.string.search_failed),Toast.LENGTH_SHORT).show();


        advanced_setup_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(cet_address.getText())) {
                    Toast.makeText(MangoAssociationActivity.this, getString(R.string.hint_mid), Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mData == null) {
                    return;
                }

                List<MortgageAssociation.Team> localTeam = mData.getTeamList();
//                teams = localTeam;
                if (localTeam == null || localTeam.size() == 0) {
                    return;
                }
                List<MortgageAssociation.Team> results = new ArrayList<>();
                for (MortgageAssociation.Team team : localTeam) {
                    if (team.getCode().equals(cet_address.getText())) {
                        results.add(team);
                    }
                }
                teams.clear();
                teams.addAll(results);
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    //        });
    private void initAdapter() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new SpacesItemDecoration(15));
        mAdapter = new Adapter();
        recyclerView.setAdapter(mAdapter);
    }

    private List<MortgageAssociation.Team> teams = new ArrayList<>();

    private class Adapter extends RecyclerView.Adapter<ViewHolder> {

//        public Adapter(List<MortgageAssociation.Team> teams){
//            this.teams = teams;
//        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(MangoAssociationActivity.this)
                    .inflate(R.layout.item_mango_team, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
//            List<MortgageAssociation.Team> data = mData.getData().getTeamList();
            holder.tv_code.setText(teams.get(position).getCode());

            holder.tv_code.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                    ClipData mClipData = ClipData.newPlainText("code", holder.tv_code.getText());
                    if (cm != null) {
                        cm.setPrimaryClip(mClipData);
                        Toast.makeText(MangoAssociationActivity.this, getString(R.string.has_coppied), Toast.LENGTH_SHORT).show();
                    }
                }
            });
            holder.tv_teamAddress.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                    ClipData mClipData = ClipData.newPlainText("address", holder.tv_teamAddress.getText());
                    if (cm != null) {
                        cm.setPrimaryClip(mClipData);
                        Toast.makeText(MangoAssociationActivity.this, getString(R.string.has_coppied), Toast.LENGTH_SHORT).show();
                    }
                }
            });
            holder.tv_teamAddress.setText(teams.get(position).getTeamAddress());
            holder.tv_contribution_mgp.setText(teams.get(position).getCoinNum() + "MGP");
            holder.tv_contribution_mgp_usd.setText("$" + teams.get(position).getCoinValue());
            List<String> roles = teams.get(position).getRoleList();
            if (roles != null) {
                StringBuilder sb = new StringBuilder();
                for (String role : roles) {
                    sb.append("(");
                    sb.append(role);
                    sb.append(")");
                }
                holder.tv_roles.setText(sb.toString());
            }

            //            if(!TextUtils.isEmpty(data.get(position).getLevelName())) {
//                holder.key.setText(data.get(position).getMid() + "[" + data.get(position).getLevelName() + "]");
//            }else{
//                holder.key.setText(data.get(position).getMid());
//            }
//            holder.value.setText(data.get(position).getChildAddress());
        }

        @Override
        public int getItemCount() {
            if (teams == null) {
//                    || mData.getData() == null
//                    || mData.getData().getTeamList() == null) {
                return 0;
            }
            return teams.size();
        }
    }

//    private class Adapter extends RecyclerView.Adapter<ViewHolder> {
//
//        @NonNull
//        @Override
//        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//            View view = LayoutInflater.from(MangoAssociationActivity.this)
//                    .inflate(R.layout.item_mango_id, parent, false);
//            return new ViewHolder(view);
//        }
//
//        @Override
//        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
//            List<MangoAssociation.MIDItem>  data = mData.getData().getData();
//            if(!TextUtils.isEmpty(data.get(position).getLevelName())) {
//                holder.key.setText(data.get(position).getMid() + "[" + data.get(position).getLevelName() + "]");
//            }else{
//                holder.key.setText(data.get(position).getMid());
//            }
//            holder.value.setText(data.get(position).getChildAddress());
//        }
//
//        @Override
//        public int getItemCount() {
//            if(mData == null){
//                return 0;
//            }
//            if(mData.getData() == null){
//                return 0;
//            }
//
//            if(mData.getData().getData() == null){
//                return 0;
//            }
//            return mData.getData().getData().size();
//        }
//    }

    private class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_code, tv_teamAddress, tv_contribution_mgp,
                tv_contribution_mgp_usd, tv_roles;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_code = itemView.findViewById(R.id.tv_code);
            tv_teamAddress = itemView.findViewById(R.id.tv_teamAddress);
            tv_contribution_mgp = itemView.findViewById(R.id.tv_contribution_mgp);
            tv_contribution_mgp_usd = itemView.findViewById(R.id.tv_contribution_mgp_usd);
            tv_roles = itemView.findViewById(R.id.tv_roles);
        }
    }
}
